package com.cumt.threeway.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.activity.DetailActivity;
import com.cumt.threeway.adapter.ActivitiesAdapter;
import com.cumt.threeway.adapter.NewActivitiesAdapter;
import com.cumt.threeway.model.Single;
import com.cumt.threeway.table.ActivitiesEntity;
import com.cumt.threeway.util.ThreeWayData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jingjing.Hu on 2016/4/7.
 */
public class BaseFragment extends Fragment implements AdapterView.OnItemClickListener{

    protected ListView mListView;
    protected List<Single> mList;
    private RequestQueue queue;
    protected String mUrl;
    private int mType = 1;
    protected List<ActivitiesEntity> mLocalList = new ArrayList<>();

    public static BaseFragment newInstance(){
        return new BaseFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, null,false);
        initView(view);
        initLocalData(mType);
//        initData(mUrl);
        return view;
    }

    private void initView(View view) {
        mList = new ArrayList<Single>();
        mListView = (ListView) view.findViewById(R.id.lv);
        mListView.setOnItemClickListener(this);
    }

    public void initData(String url) {
        queue = Volley.newRequestQueue(getActivity());
//        String url = URLS.IP + "NetServlet/SingleData";
        // 集合==》json数组
        final Gson g = new Gson();
        JsonArrayRequest jarsingle;
        jarsingle = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                String data = response.toString();
                mList = g.fromJson(data, new TypeToken<List<Single>>() {}.getType());
                ActivitiesAdapter adapter = new ActivitiesAdapter(mList, getActivity());
                mListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        });
        queue.add(jarsingle);
    }

    public void initLocalData(int activitiesType) {
        mLocalList = ThreeWayData.getInstance().getActivitiesInfo(activitiesType);
        NewActivitiesAdapter newActivitiesAdapter = new NewActivitiesAdapter(mLocalList, getActivity());
        mListView.setAdapter(newActivitiesAdapter);
    }

    public void setType(int mType) {
        this.mType = mType;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Single single = mList.get(position);
//        Intent intent = new Intent(getActivity(), SubmitActivity.class);
//        intent.putExtra("single", single);
//        startActivity(intent);
        ActivitiesEntity activitiesEntity = mLocalList.get(position);
        Intent intent = new Intent(getActivity(), DetailActivity.class);
        intent.putExtra("single", (Serializable) activitiesEntity);
        startActivity(intent);
    }
}


